<template>
    <div class="register-container">
      <div class="register-card animate__animated animate__fadeInUp">
        <div class="logo">
          <img src="@/assets/imgs/logo.png" alt="Logo">
          <h1>注册 NebulaNode</h1>
        </div>
        
        <el-form 
          :model="registerForm" 
          :rules="registerRules" 
          ref="registerFormRef"
          @keyup.enter="handleRegister"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              clearable
            ></el-input>
          </el-form-item>
          
          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              prefix-icon="Message"
              clearable
            ></el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
              clearable
            ></el-input>
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              prefix-icon="Lock"
              show-password
              clearable
            ></el-input>
          </el-form-item>
          
          <el-form-item prop="captcha">
            <div class="captcha-container">
              <el-input
                v-model="registerForm.captcha"
                placeholder="请输入验证码"
                prefix-icon="Key"
                style="width: 60%"
              ></el-input>
              <img 
                :src="captchaUrl" 
                class="captcha-img" 
                @click="refreshCaptcha"
                alt="验证码"
              >
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              @click="handleRegister" 
              :loading="loading"
              style="width: 100%"
            >
              注册
            </el-button>
          </el-form-item>
          
          <el-form-item style="text-align: center">
            <el-link type="primary" @click="goToLogin">已有账号? 立即登录</el-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import request from '@/utils/request'
  
  const router = useRouter()
  
  // 注册表单
  const registerForm = reactive({
    username: '',
    email: '',
    password: '',
    confirmPassword: '',
    captcha: ''
  })
  
  const registerRules = reactive({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    email: [
      { required: true, message: '请输入邮箱', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
      { validator: validatePassword, trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请再次输入密码', trigger: 'blur' },
      { validator: validateConfirmPassword, trigger: 'blur' }
    ],
    captcha: [
      { required: true, message: '请输入验证码', trigger: 'blur' }
    ]
  })
  
  // 状态
  const loading = ref(false)
  const captchaUrl = ref('')
  const registerFormRef = ref(null)
  
  // 自定义验证规则
  function validatePassword(rule, value, callback) {
    if (value.length < 6) {
      callback(new Error('密码长度不能少于6位'))
    } else if (!/[A-Z]/.test(value)) {
      callback(new Error('密码必须包含至少一个大写字母'))
    } else if (!/[a-z]/.test(value)) {
      callback(new Error('密码必须包含至少一个小写字母'))
    } else if (!/\d/.test(value)) {
      callback(new Error('密码必须包含至少一个数字'))
    } else {
      callback()
    }
  }
  
  function validateConfirmPassword(rule, value, callback) {
    if (value !== registerForm.password) {
      callback(new Error('两次输入的密码不一致'))
    } else {
      callback()
    }
  }
  
  // 方法
  const handleRegister = async () => {
    try {
      await registerFormRef.value.validate()
      loading.value = true
      
      // 模拟注册请求
      const res = await request.post('/api/register', {
        username: registerForm.username,
        email: registerForm.email,
        password: registerForm.password,
        captcha: registerForm.captcha
      })
      
      // 模拟响应
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } catch (error) {
      console.error('注册失败:', error)
      refreshCaptcha()
    } finally {
      loading.value = false
    }
  }
  
  const refreshCaptcha = () => {
    // 模拟验证码刷新
    captchaUrl.value = `https://picsum.photos/120/40?random=${Date.now()}`
  }
  
  const goToLogin = () => {
    router.push('/login')
  }
  
  // 初始化验证码
  refreshCaptcha()
  </script>
  
  <style scoped>
  .register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  }
  
  .register-card {
    width: 450px;
    padding: 40px;
    background: white;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  }
  
  .logo {
    text-align: center;
    margin-bottom: 30px;
  }
  
  .logo img {
    width: 80px;
    height: 80px;
    margin-bottom: 10px;
  }
  
  .logo h1 {
    margin: 0;
    font-size: 24px;
    color: #2c3e50;
  }
  
  .captcha-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  
  .captcha-img {
    width: 35%;
    height: 40px;
    cursor: pointer;
    border-radius: 4px;
    margin-left: 10px;
  }
  
  @media (max-width: 768px) {
    .register-card {
      width: 90%;
      padding: 20px;
    }
  }
  </style>